/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : AddFunction.h
* Author             : Fortiortech Appliction Team
* Version            : V1.0
* Date               : 10-Apr-2017
* Description        : This file contains all the common data types used for
*                      Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef __AddFuntion_H_
#define __AddFuntion_H_

/* Exported types -------------------------------------------------------------------------------*/
typedef struct
{
    //Current protect
		uint8		OverCurCnt;          	  // 软件过流计数

		uint16      Abs_ia;				      // IA的绝对值
		uint16      Abs_ib;				      // IB的绝对值
		uint16      Abs_ic;				      // IC的绝对值

		uint16      Max_ia;				      // IA的最大值
		uint16      Max_ib;				      // IB的最大值
		uint16      Max_ic;				      // IC的最大值
}CurrentVarible;
typedef struct
{
    uint16      SecondStartTimes;   // 二次启动保护的次数
  	uint8       StartFlag;          // 启动保护的标志位，用于判断哪个方法起作用
		uint16      StallTimes;	        // 堵转保护次数
		uint8       StallFlag;          // 堵转保护的标志位，用于判断哪个方法起作用
		uint16      LossPHTimes;        // 缺相保护次数
		uint16      CurrentPretectTimes;// 过流保护次数
}ProtectVarible;
typedef struct
{
    uint16 			OverVoltDetecCnt; // 过压检测计数
    uint16 		  UnderVoltDetecCnt;// 欠压检测计数
		uint16      VoltRecoverCnt;	  // 过压恢复计数
    uint16      OverCurrentCnt;     // 过流检测计数
    uint16      OverCurrentCnt1;    // 过流检测计数
    uint16      OverCurrentCnt2;    // 过流检测计数
    uint16      OverCurrentCnt3;    // 过流检测计数
    uint16      OverCurrentCnt4;    // 过流检测计数	
	  uint16      MosTemperCnt;     // Mos温度判断计数
	   uint16      MosTemperCnt1;     // Mos温度判断计数
    uint16      MosTemperRecover; // Mos温度恢复判断计数
	  uint16      nFAULTDelayCnt;
    uint16      BATTemperCnt;     // Bat温度判断计数
    uint16      BATTemperRecover; // Bat温度恢复判断计数
    uint16      MOTTemperCnt;     // MOT温度判断计数
    uint16      MOTTemperRecover; // MOT温度恢复判断计数	
}FaultVarible;

typedef enum
{
	  FaultNoSource				= 0,        // 无故障
		FaultHardOVCurrent  = 1,	      // 硬件过流
    FaultSoftOVCurrent	= 2,        // 一级软件过流
    FaultSoftOVCurrent1	= 3,        // 二级软件过流
    FaultSoftOVCurrent2	= 4,        // 三级软件过流
    FaultSoftOVCurrent3	= 5,        // 四级软件过流	
	  FaultSoftOVCurrent4	= 6,        // 五级软件过流
    FaultUnderVoltage	  = 7,	      // 欠压保护
    FaultOverVoltage    = 8,        // 过压保护
		//FaultLossPhase   		= 9,        // 缺相保护
		FaultMosUnderTemperature =9,
		FaultMosOverTemperature = 10,   //MOS过温保护
  
	  FaultHALL            = 11,//13,      //霍尔故障
	  FaultHALL_Stall      = 12,//14,      //霍尔堵转故障		
    FaultBATOverTemperature = 13,//11,	  //BAT过温保护
	  FaultMOTOverTemperature =14,// 12,   //MOT过温保护
	  Faultnfault          = 15,
} FaultStateType;


typedef struct
{
  //  uint16      ON_Count;           // 运行时间计数
  //  uint16      OFF_Count;          // 停止时间计数
  //  uint16      ONOFF_Times;        // 启停次数
  //  uint8       ONOFF_Flag;         // 启停测试中启动标志位
    uint16      PowerOFF_Count;            
} ONVarible;
typedef struct
{
		uint16 LedCount;                // 闪灯周期计数
		uint8  LedTimCot;               // 闪灯次数计数
		uint16 Counttime;               // 闪灯周期
		uint16 Count;                   // 电平翻转计数
}MCLedDisplay;
typedef struct
{
 uint16 LimitInCnt;
    uint16 LimitExitCnt;// add by  shiyan
    
	  uint16 mcDcbusFlt;              // 母线电压
    uint16 MosTempDecFlt;
    uint16 BATTempDecFlt;	
	  uint16 MOTTempDecFlt;	
	  uint16 mcADCCurrentbus;        //电流保护
    uint16 ChargeStep;              // 预充电的步骤
    uint16 State_Count;	            // 电机各个状态的时间计数

}FOCCTRL;

typedef struct
{
  uint8  Status;              
  uint8  Section; 
  uint8  Angle_flage;	
  uint16 Angle_Count;		
  uint8  Delay_flage;	
  uint16 Delay_Count;		
  uint16 ChangePhaseNum;  
  uint16 BCCR_Angle;		
  uint16 BCCR_Angle_Value;		
  uint16 DelayAngleOut;	
	uint16 DelayAngleIn;
	uint16 DelayAngleDEC;
 
} HALL_TypeDef;

typedef struct
{
   uint8  StepCur;                
	 uint8  StepPre;
} MCL_TypeDef;
  
typedef struct
{
	  uint32 SleepDelayCout;
	  int32  SleepFlag;
    uint8  SleepEn;
}SLEEPMODE;

typedef struct
{
		uint16 Read;
		uint16 Sum;
		uint16 RealValue;
}VspInput;

typedef struct
{
		uint16  TargetValue;
		uint16  ActualValue;
	  uint16  BLDC_Value;

	  int16  DelayPeriod;
		int8   FlagONOFF;
	  int16  Ramptime;
	  int16  DRVCOUNT;
	  int16  Ramptime_Inc;
		int16  DUTY_IncValue;
	  int16  Duty_target;
	  int16  Duty_END;
	  int8   State_flag;
}MCRAMP;
typedef union 
 {
   uint32 PWMCompareAMP;// 比较值标幺化的值
   uint16 PWMCompareUpdate[2];
 }PWMD;
typedef struct
{
		uint16 PWMCompare;   // 比较值
		uint16 PWMARR;       // 周期值
		uint16 PWMCompareOld;// 上一次的比较值  
    PWMD   pwm;          // 使用共同体，将比较值乘以32768
	  uint16 PWMARROld;    // 上一次的周期值
	  uint16 PWMFlag;      // PWM标志位，置1期间不更新duty
    uint16 PWMARRUpdate; // 计算中的周期值
	  uint16 PWMDuty;      // PWM占空比，Q15格式
	  int16 PWMUpdateFlag; // PWM新的duty更新
  	uint32 PWMVSum;      // PWM的总和
	  uint16 PWMcnt;       // PWM求平均技术
    uint8  PWMDivFlag;   // PWM的除法标志位

}PWMINPUTCAL;

typedef struct
{
	  uint8 FR;          //当前给定的转子方向
	  uint8 FlagFR;      //反向的标志位
	  uint8 FRStatus;
	  uint8	TargetFR;    //设置的目标方向
	  uint16 OldTargetSpeed;
} MotorFRTypeDef;

typedef struct
{
		uint32 TEST1;
		uint16 TEST2;
		uint16 TEST3;
		int16 TEST4;
		int16 TEST5;
		int16 TEST6;
		int32 TEST7;
		uint16 TEST8;
		uint16 TEST9;
}testInput;

typedef enum
{ 
	master_           =0,
	left_motor_      = 1,        
	right_motor_     =2,
	cut_motor_       =3, 
	failed_           =4,
} BoardIdType;
typedef struct 
{
//  uint8 header_;         // 0x7e
  uint8 fromId_;         // master: 0; left:1; right:2 ; cut: 3
  uint8 left_control_;  //   stop: 0,forward: 1, back:2, read_only: 3,ignore:4
  uint8 left_speed_h;    //  front: +; back: -; 
  uint8 left_speed_l;
  uint8 right_control_; //   stop: 0,forward: 1, back:2, read_only: 3,ignore:4
  uint8 right_speed_h; 
  uint8 right_speed_l; 
  uint8 cut_state_;  // stop：0；  start:1; read_only: 3,  ignore:4 
 // uint8 cut_speed_;  // 30-100
 // uint8 reserve;//chk_code_;
//  uint8 tail_;  //  0x7f
} stUsmdControlCmd;

typedef struct 
{
//  uint8 header_;         // 0x7e  
  uint8 from_left_right_motor_;    //  left:1; right:2 ,cut : 3
 // uint8 control_;  // stop:0, forward: 1, back:2, read_only: 3, ignore:4, stop_read:5, init_circle: 6
  uint8 speed_h;    //  front: +; back: -; 
  uint8 speed_l;
 // uint8 cut_state_;
 // uint8 cut_speed_;
  uint8 errorCode; //  0 no fault , 1 2 3 ????
  uint8 circleCount_h;  //  circleCount/6/2/139
  uint8 circleCount_mh;//  
  uint8 circleCount_ml;
  uint8 circleCount_l;
//  uint8 reserve2;
//  uint8 reserve3;
//  uint8 tail_;  //  0x7f
} stResControlCmd;

//typedef struct 
//{
////  uint8 header_;         // 0x7e  
//  uint8 from_left_right_motor_;    //  left:1; right:2 ,cut : 3
//  uint8 control_;  // stop:0, forward: 1, back:2, read_only: 3, ignore:4
//  uint8 speed_h;    //  front: +; back: -; 
//  uint8 speed_l;
// // uint8 cut_state_;
// // uint8 cut_speed_;
//  uint8 errorCode; //  0 no fault , 1 2 3 ????
//  uint8 reserve;//chk_code_;
//  uint8 reserve1;
//  uint8 reserve2;
////  uint8 reserve3;
////  uint8 tail_;  //  0x7f
//} stResControlCmd;
/* Exported variables ---------------------------------------------------------------------------*/
extern PWMINPUTCAL      xdata     mcPwmInput;
extern FaultVarible     xdata 		mcFaultDect;
extern CurrentVarible   xdata     mcCurVarible;
extern ProtectVarible   xdata     mcProtectTime;
extern ONVarible        xdata     ONOFFTest;
extern FaultStateType/*	xdata*/	 	mcFaultSource;
extern FaultStateType /*  xdata	*/	oldmcFaultSource;
extern FOCCTRL          xdata 		mcFocCtrl;
extern MCLedDisplay     xdata     mcLedDisplay;
extern MCRAMP           xdata     mcSpeedRamp;
extern int16						xdata     mcSpeedFdb;
extern uint16 					xdata     VSP;
extern MotorFRTypeDef		xdata     mcFRState;
extern SLEEPMODE        xdata     SleepSet;

extern stUsmdControlCmd xdata  stUsrConCmd;
extern stResControlCmd  xdata  stResConCmd;
extern BoardIdType     xdata  boardid;
extern uint16 uTargetValue;

extern uint8       /*     xdata */  Time_1ms;
//extern uint16   timer_1ms;

extern uint8 can_spi_rcv[13];
extern uint8 can_spi_tx[13];
extern uint16  commandCount;
extern uint8   spiT_rcv_end_flag;
extern uint16 /*xdata*/   HALL_Stall_time;
extern uint16  commandCount_can;

/* Exported functions ---------------------------------------------------------------------------*/

extern void Fault_MosTemperature(FaultVarible *h_Fault);
extern void Fault_HALL_Stall(void);
extern void Fault_BATTemperature(FaultVarible *h_Fault);
extern void Fault_MOTTemperature(FaultVarible *h_Fault);
extern void Fault_OverUnderVoltage(FaultVarible *h_Fault);
extern void Fault_Overcurrent(FaultVarible *h_Fault);
extern void Fault_Stall(FaultVarible *h_Fault);
extern void Fault_phaseloss(FaultVarible *h_Fault);
extern void VariablesPreInit(void);
extern void Fault_Detection(void);
extern  void Fault_can_spi_write_conflict(FaultVarible *h_Fault);

extern int16 KLPF_VALUE(int16 INVlaue, int16 OutLastValue);
extern void Speed_response(void);
extern void mc_ramp(MCRAMP *hSpeedramp);
extern void FGOutput(void);
extern void VSPSample(void);
extern void FRFunction(void);
extern void HW_Speed_PI(void);
extern int16 HW_Genal_PI(int16 Xn1, int16 Yn0, int16 Xn2);
extern int16 HW_One_PI(int16 Xn1);
extern  int16 SoftLPF(int16 Xn1, int16 Xn0, int16 K);
extern void Sleepmode(void);
extern void LED_Display(void);
extern void Led_OnOff(MCLedDisplay *hLedDisplay,uint8 htime);
extern void PDELAY_functions(void);
extern void FaultProcess(void);
extern void SpeedRegulation(void);
extern int16 LPFFunction(int16 Xn1, int16 Xn0, int8 K);
extern uint32 Abs_F32(int32 value);
extern uint16 Abs_F16(int16 value);
extern void StarRampDealwith(void);
extern void ONOFF_StartTest(ONVarible  *h_test);
extern void MULDIV_test(void);
extern void APP_DIV(void);
extern void LED_signal (void);
extern void Fault_nFault(FaultVarible *h_Fault);
extern void TailWindSpeedDetect(void);

extern void PWMInputCapture(void);
extern void RDOutput(void);
extern void Decide_keypower(void);

extern void boardId__(void);
extern void delay_ms(uint16 count);
extern void delay_us(uint16 count);

extern void Can_Send(uint8 *msg, uint8 len);
extern void Can_Recv(void);

extern void checkSpiData__(void);
extern void SendResMsg(void);

#endif